﻿using System;
using System.Text;
using System.Text.RegularExpressions;

using BizElements.Core.ProviderComponents;

namespace BizElements.Core.Oracle
{
    sealed class OracleTableRenderer : ITableRenderer
    {
        public void RenderTableName(IDbTable table, StringBuilder output)
        {
            if (IsNonquotedIdentifier(table.TableName))
            {
                // Don't quote alfanumeric identifiers because in that case Oracle is case sensitive
                output.Append(table.TableName);
            }
            else
            {
                // Special characters must be enclosed in quotes.
                output.Append("\"");
                output.Append(table.TableName);
                output.Append("\"");
            }
        }

        public void RenderAlias(IDbTable table, StringBuilder output)
        {
            if (IsNonquotedIdentifier(table.Alias))
            {
                // Don't quote alfanumeric identifiers because in that case Oracle is case sensitive
                output.Append(table.Alias);
            }
            else
            {
                // Special characters must be enclosed in quotes.
                output.Append("\"");
                output.Append(table.Alias);
                output.Append("\"");
            }
        }

        static readonly Regex NonquotedIndetifierFormat = new Regex("^[a-zA-Z_]*[a-zA-Z_0-9\x2E]*$");

        internal static bool IsNonquotedIdentifier(string oraObjectIdentifier)
        {
            return NonquotedIndetifierFormat.IsMatch(oraObjectIdentifier);
        }
    }
}